package com.sanjiahantang.youjiaclient.module.complain.presenters;

import android.content.Context;

import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.complain.adapters.ComplainAdapter;
import com.sanjiahantang.youjiaclient.module.complain.beans.ComplainBean;
import com.sanjiahantang.youjiaclient.module.complain.models.IComplainModel;
import com.sanjiahantang.youjiaclient.module.complain.models.impl.ComplainModel;
import com.sanjiahantang.youjiaclient.module.complain.views.MeComplainView;
import com.sanjiahantang.youjiaclient.module.complain.views.impl.ComplainActivity;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PARAM;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TOKEN;


/**
 * Created by zy on 2017/9/8.
 */

public class MeComplainPresenter extends BaseMvpPresenter<MeComplainView>  {

    private IComplainModel complainModel;

    public MeComplainPresenter(){
        complainModel = new ComplainModel();
    }

    public void loadData(Context context, int type) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put(KEY_PAGE,0);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        Map<String,Object> map = new HashMap<>();
        mView.showProgress();
        param.put(KEY_PARAM,map);
        if (type == MeComplainView.TYPE_FROM_ME){
            map.put(MeComplainView.KEY_COMPLAIN_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
            complainModel.findCustomerComplaint(context, param, new CallBack<ComplainBean>() {
                @Override
                public void onSuccess(ComplainBean response, Context context, Object tag) {
                    if (mView == null) return;
                    hideProgress();
                    mView.loadData(new ComplainAdapter(context,response,MeComplainView.TYPE_FROM_ME));
                }

                @Override
                public void onFailure(String message) {
                    hideProgress();
                }
            });
        }else if (type == MeComplainView.TYPE_TO_ME){
            map.put("complaintObjectLoginId", SPUtils.get(context, SPConfig.PHONE,"").toString());
            complainModel.findCustomerBeComplaint(context, param, new CallBack<ComplainBean>() {
                @Override
                public void onSuccess(ComplainBean response, Context context, Object tag) {
                    if (mView == null) return;
                    hideProgress();
                    mView.loadData(new ComplainAdapter(context,response,MeComplainView.TYPE_TO_ME));
                }

                @Override
                public void onFailure(String message) {
                    hideProgress();
                }
            });
        }

    }

    private void hideProgress() {
        mView.hideProgress();
    }


    public void addComplain(Context context) {
        goPageSimply(context, ComplainActivity.class);
    }
}
